//
//  CXHotViewController.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/26.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXHotViewController.h"
#import "CXTitleView.h"
#import "CXHotViewModel.h"

#import "CXFirstCollectionViewCell.h"
#import "CXSecondCollectionViewCell.h"
#import "CXDestinationTableViewCell.h"
#import "CXZJDetailView.h"

#import "CXDestinationViewController.h"
#import "CXSpecialMoreViewController.h"

static NSString * const CXFirstCell = @"firstCell";
static NSString * const CXSecondCell = @"secondCell";

@interface CXHotViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, CXTitleViewDelegate, SDCycleScrollViewDelegate>

@property (nonatomic, strong) CXHotViewModel * hotViewModel;

@property (nonatomic, weak) UICollectionView * collectionView;

@property (nonatomic, strong) NSString * type;

@property (nonatomic, assign) NSInteger categoryID;

@property (nonatomic, strong) UIImageView * imageView;

@end

@implementation CXHotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:243 / 255.0 blue:247 / 255.0 alpha:1.0];
    [self.collectionView.mj_header beginRefreshing];
}

#pragma mark - headView
//创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                withReuseIdentifier:@"UICollectionViewHeader"
                                                                                       forIndexPath:indexPath];
        
        NSMutableArray * imageUrl = [[NSMutableArray alloc] init];
        
        for (NSInteger i = 0; i < self.hotViewModel.focusImgNumber; i++) {
            NSString * str = [NSString stringWithFormat:@"%@",[self.hotViewModel focusImgURLWithIndex:i]];
            [imageUrl addObject:str];
            
            NSLog(@"%@",str);
        }
        
        SDCycleScrollView * scrollerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, CXScreenWidth, CXScreenHeight / 4) imageNamesGroup:imageUrl];
        
        scrollerView.layer.masksToBounds = YES;
        scrollerView.layer.cornerRadius = 4;
        scrollerView.delegate = self;
        scrollerView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        scrollerView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        [headView addSubview:scrollerView];
        
        return headView;
        
    }else{
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                withReuseIdentifier:@"newHeadView"
                                                                                       forIndexPath:indexPath];
        
        CXTitleView * moreView = [[CXTitleView alloc] initWithTitle:[self.hotViewModel mainTitleWithSection:indexPath.section] hasMore:[self.hotViewModel hasMoreWithSection:indexPath.section]];
        moreView.delegate = self;
        moreView.tag = indexPath.section;
        moreView.frame = headView.bounds;
        [headView addSubview:moreView];
        return headView;
    }
}

#pragma mark - 图片点击事件
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"%ld",(long)index);
}


// 设置section头视图的参考大小，
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeMake(CXScreenWidth, CXScreenHeight / 4);
    }else return CGSizeMake(0, 40);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
        return CGSizeMake(CXScreenWidth - 20, 100);
    }else{
        return  CGSizeMake((kScreenWidth - 60) / 3 , 125);
    }
    
}

#pragma mark - collection 数据源

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.hotViewModel.section - 4;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else if (section == 1) {
        return self.hotViewModel.specialCount;
    }else if(section >=2 && section <= 22){
        return [self.hotViewModel hotRecommendsCount:section - 2];
    }else{
        return 1;
    }
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
        CXSecondCollectionViewCell * secondCell = [collectionView dequeueReusableCellWithReuseIdentifier:CXSecondCell forIndexPath:indexPath];
        [secondCell.imageView setImageWithURL:[self.hotViewModel coverURLWithSection:indexPath.section index:indexPath.item] placeholderImage:kGetImage(@"cell_bg_niData_2")];
        secondCell.titleLabel.text = [self.hotViewModel titleWithSection:indexPath.section index:indexPath.item];
        secondCell.subTitleLabel.text = [self.hotViewModel trackTitleWithSection:indexPath.section index:indexPath.item];
        return secondCell;
    }
    
    CXFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CXFirstCell forIndexPath:indexPath];
    
    [cell.imageView setImageWithURL:[self.hotViewModel coverURLWithSection:indexPath.section index:indexPath.row] placeholderImage:kGetImage(@"cell_bg_noData_6")];
    
    cell.titleLabel.text = [self.hotViewModel titleWithSection:indexPath.section index:indexPath.row];
    
    return cell;
    
}


#pragma mark - collectionViewCell 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 1) {
        CXDestinationViewController * destinationVC = [[CXDestinationViewController alloc] initWithAlbumId:[self.hotViewModel albumIdWithSection: indexPath.section index:indexPath.item] title:[self.hotViewModel mainTitleWithSection:indexPath.section]];
        
        [self.navigationController pushViewController:destinationVC animated:YES];
    }
    
    if (indexPath.section > 1) {
        CXDestinationViewController * destinationVC = [[CXDestinationViewController alloc] initWithAlbumId:[self.hotViewModel albumIdWithSection:indexPath.section index:indexPath.item] title:[self.hotViewModel mainTitleWithSection:indexPath.section]];
        
        [self.navigationController pushViewController:destinationVC animated:YES];
    }
    
    NSLog(@"%ld",indexPath.section);
    NSLog(@"%ld",indexPath.row);
}




#pragma mark - DSTitleViewDelegate

-(void)titleViewDidClick:(NSInteger)tag{
    
    _categoryID = [self.hotViewModel categoryIdWithSection:tag];
    _type = [self.hotViewModel contentTypeWithSection:tag];
    
    switch (tag) {
        case 1:{
            CXSpecialMoreViewController *vc = [[CXSpecialMoreViewController alloc] init];
            // push 跳转控制器 隐藏tabbar
            vc.hidesBottomBarWhenPushed = YES;
            self.navigationController.navigationBar.hidden = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}


#pragma mark - 懒加载
-(CXHotViewModel *)hotViewModel{
    if (!_hotViewModel) {
        _hotViewModel = [[CXHotViewModel alloc] init];
    }
    return _hotViewModel;
}

-(UIImageView *)imageView{
    
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}



-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        flowLayout.minimumLineSpacing = 1.0f;
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.itemSize = CGSizeMake((kScreenWidth - 60) / 3 , 125);
        flowLayout.headerReferenceSize = CGSizeMake(CXScreenWidth, CXScreenHeight / 4);
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 0, 20);
        
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CXScreenWidth, CXScreenHeight - 35 - 49 - 64) collectionViewLayout:flowLayout];
        [self.view addSubview:collectionView];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CXFirstCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:CXFirstCell];
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CXSecondCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:CXSecondCell];
        
        
        
        collectionView.bounces = YES;
        //注册头视图
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
        
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"newHeadView"];
        self.collectionView = collectionView;
        
        // 上拉刷新
        collectionView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            [self.hotViewModel getDataCompletionHandle:^(NSError *error) {
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView reloadData];
            }];
        }];
    }
    
    return _collectionView;
}



@end
